class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int n = nums.size();
        vector<int> sum(n+1);
        int ret = INT_MAX;
        
        for(int i = 1;i <= n;i++){
            sum[i] = sum[i-1] + nums[i-1];
        }
        
        for(int i = 1;i <= n;i++){
            int t = target + sum[i-1];
            
            auto ans = lower_bound(sum.begin(),sum.end(),t);
            
            if(ans != sum.end()){
                ret = min(ret,static_cast<int>(ans-sum.begin() - (i-1)));
            }
        }
        return ret == INT_MAX ? 0 : ret;
    }
};
